import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/module_permissions_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/securtity_base_drawer.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/double_prevention_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/daily_commitment/daily_commitment_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/equipment_start_shutdown_ledger/equipment_start_shutdown_ledger_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/home/home_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/instrument_monitoring_ledger_page/instrument_monitoring_ledger_page_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_security_identification_page/risk_security_identification_view.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/task_implementation_page/task_implementation_view.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///风险管控
class RiskControlController extends GetxController {
  /// 外部controller
  final applyController = Get.find<DoublePreventionController>();

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 展开收起控制key
  final scaffoldKey = GlobalKey<ScaffoldState>();

  /// 选中的页面
  int selectIndex = 0;

  final titleList = [
    SecurityBaseEntity(
      title: '总览',
      icon: AppImages.icon_risk_overview,
      expandTitle: '总览',
      currentPageTitle: 0,
      pages: [RiskManagementHomePage()],
      path: '/home',
    ),
    SecurityBaseEntity(
      title: '每日承诺',
      icon: AppImages.icon_risk_security_identification,
      expandTitle: '每日承诺',
      currentPageTitle: 0,
      pages: [DailyCommitmentPage()],
      path: '/dailyCommitment',
    ),
    SecurityBaseEntity(
      title: '安全风险辨识',
      icon: AppImages.icon_risk_security_identification,
      expandTitle: '安全风险辨识',
      currentPageTitle: 0,
      pages: [RiskSecurityIdentificationPage()],
      path: '/riskSecurity',
    ),
    SecurityBaseEntity(
      title: '风险管控清单',
      icon: AppImages.icon_education_train_plan,
      expandTitle: '风险管控清单',
      currentPageTitle: 0,
      pages: [TaskImplementationPage()],
      path: '/taskImplementation',
    ),
    SecurityBaseEntity(
      title: '仪表监测台账',
      icon: AppImages.icon_education_demand_research,
      expandTitle: '仪表监测台账',
      currentPageTitle: 0,
      pages: [InstrumentMonitoringLedgerPagePage()],
      path: '/monitoringLedger',
    ),
    SecurityBaseEntity(
      title: '大检修台账',
      icon: AppImages.icon_education_demand_research,
      expandTitle: '装置开停车大检修台账',
      currentPageTitle: 0,
      pages: [EquipmentStartShutdownLedgerPage()],
      path: '/overhaulLedger',
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    initModule();
  }

  /// 根据后台权限初始化菜单
  void initModule() {
    final roleMenuList = applyController.roleMenuMap[AppRoutes.riskControl];
    ModulePermissionsUtil.removeRoleMenu(titleList: titleList, menuTreeList: roleMenuList);
    update();
  }

  /// title选择切换
  /// [titleIndex] 标题索引
  /// [index] 页面索引
  void selectTitle(int titleIndex, int index) {
    titleList[titleIndex].currentPageTitle = index;
    update();
  }

  /// 点击左侧菜单
  /// [index] 索引
  void selectLeftMenu(int index) {
    selectIndex = index;
    update();
  }

  /// 点击展开菜单
  /// [index] 索引
  void selectExpandMenu(int index) {
    selectIndex = index;
    Get.back();
    update();
  }

  /// 筛选界面
  // Rx<Widget> filterWidget=Container().obs ;
  ///在 Flutter 中使用 Rx<Widget> 是可能的，但通常不建议这样做，因为它违背了 Flutter 的声明式UI原则。在 Flutter 中，UI
  /// 应该是状态的反应，而不是状态本身。通常，您应该使用 Rx<T> 来跟踪应用程序的状态（如数据或用户输入），然后根据这些状态构建UI。
  /// 注意事项
  // 性能问题：频繁地创建和销毁Widget可能会导致性能问题。
  // 违背原则：这种做法违背了 Flutter 的原则，即UI应该是状态的直接反应，而非状态本身。
  // 维护难度：长期维护可能会变得复杂和困难，特别是在大型项目中
  var filterWidget = Rx<Widget>(Container());

  /// 筛选列表数据
// List<DoubleControlEntity> doubleControlList = [];
// /// 获取安全风险辨识结构树
// void _getSafeTree() async {
//
//   final res = await HttpUtil().generateGetRequest<List<DoubleControlEntity>>
//   (Interface.etRiskIdentificationTree).execute();
//
//   res.onSuccess((result) {
//     if(result.data != null) {
//       doubleControlList = result.data!;
//       update();
//     }
//   });
//
// }
//
// // 重置
// void reset() {
//
// }
//
// // 确定
// void confirm() {
//   final RiskSecurityIdentificationController controller = Get.find();
//   Get.back();
//   controller.doubleControlList = doubleControlList;
//   controller.globalKey.currentState!.onRefresh();
//   //修改当前安全风险等级level
//   controller.currentPageLevel=3;
// }
//
// @override
// void onReady() {
//   super.onReady();
//   _getSafeTree();
// }
}
